package captcha_logic

import (
	"gitee.com/Luna-CY/Golang-Project-Template/internal/interface/cache"
	"github.com/golang/freetype/truetype"
	"github.com/wenlng/go-captcha-assets/bindata/chars"
	"github.com/wenlng/go-captcha-assets/resources/fonts/fzshengsksjw"
	"github.com/wenlng/go-captcha-assets/resources/images"
	"github.com/wenlng/go-captcha/v2/base/option"
	"github.com/wenlng/go-captcha/v2/click"
)

func New(cache cache.Cache) *Captcha {
	var builder = click.NewBuilder()

	// 生成选项4-6个
	builder.SetOptions(click.WithRangeLen(option.RangeVal{Min: 4, Max: 6}))
	// 验证选项3-4个
	builder.SetOptions(click.WithRangeVerifyLen(option.RangeVal{Min: 3, Max: 4}))

	// 设置验证码字符集
	builder.SetResources(click.WithChars(chars.GetChineseChars()))

	// 设置验证码字体资源
	fonts, _ := fzshengsksjw.GetFont()
	builder.SetResources(click.WithFonts([]*truetype.Font{fonts}))

	// 设置验证码背景图资源
	bgs, _ := images.GetImages()
	builder.SetResources(click.WithBackgrounds(bgs))

	return &Captcha{
		cache:   cache,
		captcha: builder.Make(),
	}
}

type Captcha struct {
	cache   cache.Cache
	captcha click.Captcha
}
